// ElGamal implementation by Mike Austin

#include <stdio.h>
#include <cstdlib>
#include <time.h>
#include "ElGamal.hpp"

int main(int argc, char ** argv)
{
	unsigned int privateKey;
	unsigned int publicKey;
	unsigned int number;
	unsigned int result;

	printf("Testing ElGamal...\n\n");

	printf("Generating private key...\n");
	privateKey = generatePrivateKey();
	printf("Generated private key: %u\n\n", privateKey );

	printf("Generating public key...\n");
	publicKey = generatePublicKey( privateKey );
	printf("Generated public key: %u\n", publicKey );

	printf("\nGenerating 32-bit random data...\n");

	srand((unsigned int)time(NULL));

	struct num
	{
		short part1;
		short part2;
	} * combinedNum = (struct num *) &number;

	// need to calculate random upper 16 bits and random lower 16 bits
	// separately because rand() only goes up to 2^16-1
	combinedNum->part1 = rand() % (prime - 1) + 1;
	combinedNum->part2 = rand() % (prime - 1) + 1;

	printf("Random data = %u\n", number);

	printf("\nEncrypting data...\n");

	Ciphertext ciphertext = elGamalEncrypt( publicKey, number );
	printf("Ciphertext (c1, c2) = (%u, %u)\n", ciphertext.c1, ciphertext.c2 );

	printf("\nDecrypting data...\n");
	result = elGamalDecrypt( privateKey, ciphertext );
	printf("Plaintext = %u\n", result);

	printf("\nDone.\n");
}